/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package parser.serverData.goodlist;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import parser.clientData.clientItems.ClientItem;
import parser.clientData.goodlist.ClientNpcGoodslist;
import parser.clientData.goodlist.Data;
import parser.serverData.goodlist.GoodsList.Item;
import parser.util.DataManager;


/**
 * @author Mr. Poke
 *
 */
public class GoodListSave
{

	public static void save()
	{
		ObjectFactory objFactory = new ObjectFactory();
		Goodslists collection = objFactory.createGoodslists();
		List<GoodsList> templateList = collection.getList();

		for (ClientNpcGoodslist good : DataManager.getInstance().getGoodMap().values())
		{
			GoodsList list = new GoodsList();
			if (good.getAdvertiseMsg() != null)
				continue;
			templateList.add(list);
			list.setId(good.getId());
			if (good.getGuildLevel() != null)
				list.setGuildLvl(good.getGuildLevel());
			if (good.getGoodsList() == null)
				continue;
			for (Data data : good.getGoodsList().getData())
			{
				Item item = new Item();
				int clientItemId = DataManager.getInstance().getItemNameIdMap().get(data.getItem().toLowerCase());
				ClientItem clientItem = DataManager.getInstance().getIdItemMap().get(clientItemId);
				item.setProduction(clientItemId);
				list.getItem().add(item);
				if (good.getUseCategory() == null)
				{
					Ingredient ingredient = new Ingredient();
					ingredient.setCount(clientItem.getPrice());
					ingredient.setItemId(182400001);
					item.getIngredient().add(ingredient);
				}
				else if (good.getUseCategory() == 1)
				{
					Ingredient abyssPoint = new Ingredient();
					abyssPoint.setItemId(100);
					abyssPoint.setCount(clientItem.getAbyssPoint());
					item.getIngredient().add(abyssPoint);
					Ingredient medal = new Ingredient();
					if (clientItem.getAbyssItem() == null)
						continue;
					int medalId = DataManager.getInstance().getItemNameIdMap().get(clientItem.getAbyssItem().toLowerCase());
					ClientItem clientItemMedal = DataManager.getInstance().getIdItemMap().get(medalId);
					medal.setItemId(clientItemMedal.getId());
					medal.setCount(clientItem.getAbyssItemCount());
					item.getIngredient().add(medal);
				}
				else if (good.getUseCategory() == 2)
				{
					Ingredient coupon = new Ingredient();
					coupon.setCount(clientItem.getCouponItemCount());
					int medalId = DataManager.getInstance().getItemNameIdMap().get(clientItem.getCouponItem().toLowerCase());
					ClientItem clientItemCoupon = DataManager.getInstance().getIdItemMap().get(medalId);
					coupon.setItemId(clientItemCoupon.getId());
					item.getIngredient().add(coupon);
				}
				else if (good.getUseCategory() == 3)
				{
					Ingredient coin = new Ingredient();
					coin.setCount(clientItem.getExtraCurrencyItemCount());
					int coinId = DataManager.getInstance().getItemNameIdMap().get(clientItem.getExtraCurrencyItem().toLowerCase());
					ClientItem clientItemCoin = DataManager.getInstance().getIdItemMap().get(coinId);
					coin.setItemId(clientItemCoin.getId());
					item.getIngredient().add(coin);
				}
			}
				
		}
		try
		{
			JAXBContext jaxbContext = JAXBContext.newInstance("parser.serverData.goodlist");
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "goodslists.xsd");
			marshaller.marshal(collection, new FileOutputStream("static_data/goodslists/goodslists.xml"));
		}
		catch (PropertyException e)
		{
			e.printStackTrace();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
		}
	}

}
